package com.springboot.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.blog.beans.*;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.CommentDao;
import com.springboot.blog.service.CommentService;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private CommentDao commentDao;

    @RequestMapping("allComment")
    private String allComment(Integer page,Integer limit){

        PageBlog<BlogComment> pageBlog = commentService.page(page, limit);
        int count = commentDao.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",pageBlog);
        return jsonObject.toString();
    }

    @RequestMapping("updateCommentStatus")
    public void  updateCommentStatus(String commentId,String commentStatus){
        BlogComment blogComment = new BlogComment();
        blogComment.setCommentId(Long.parseLong(commentId));
        blogComment.setCommentStatus(Integer.parseInt(commentStatus));
        commentDao.updateById(blogComment);
    }

    //批量删除评论
    @RequestMapping("deleteComment")
    public R deleteComment(String delete){
        List<BlogComment> list2=(List<BlogComment>) JSONArray.toList(JSONArray.fromObject(delete), BlogComment.class);
        List<Long> commentList = new ArrayList<>();
        for (BlogComment comment : list2){
            commentList.add(comment.getCommentId());
        }
        int deleteComment = commentDao.deleteBatchIds(commentList);
        if (deleteComment>0) {
            return R.ok();
        }
        return R.error();
    }
    //删除评论
    @RequestMapping("deleteByIdComment")
    public R deleteByIdBlog(String commentId){
        int deletecomment = commentDao.deleteById(Long.parseLong(commentId));
        if (deletecomment>0){
            return R.ok();
        }
        return R.error();
    }

    //修改评论
    @RequestMapping("updateComment")
    public R updateComment(String commentId, String updateComment){
        BlogComment blogComment = new BlogComment();
        blogComment.setCommentId(Long.parseLong(commentId));
        blogComment.setCommentContent(updateComment);
        int update = commentDao.updateById(blogComment);
        if (update>0){
            return R.ok();
        }
        return R.error();
    }
}
